/********************************************************************************
 * Project Name		[JavaEE_Web]
 * File Name     	[TestServletResponse.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package servlet.api;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletResponse;

/**
 * javax.servlet.ServletResponse
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
@WebServlet(name = "TestServletResponse", urlPatterns = "/servletResponse")
public class TestServletResponse extends GenericServlet {

	private static final long serialVersionUID = 5913929274894902509L;

	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		// ServletResponse的相关方法
		PrintWriter out = response.getWriter();
		out.write("response....");

		HttpServletResponse httpResponse = (HttpServletResponse) response;
		// 设置缓存时间为一天
		httpResponse.setDateHeader("expires", System.currentTimeMillis() + 24 * 3600 * 1000);
		// 设置重定向
		httpResponse.sendRedirect("http://www.baidu.com");
	}

	/**
	 * Http响应头: Location<br/>
	 * 
	 * @param httpResponse
	 */
	protected void testLocationHeader(HttpServletResponse httpResponse) {
		// 设置状态码
		httpResponse.setStatus(302); // 302头表示请求资源未找到, 将转到指定的页面
		// 设置Location响应头, 实现重定向
		httpResponse.setHeader("Location", "/JavaEE_Web/hello");
	}

	/**
	 * Http响应头: Content-Type<br/>
	 * 
	 * 浏览器能接收[Accept]的数据类型有:<br/>
	 * application/x-ms-application<br/>
	 * image/jpeg<br/>
	 * application/xaml+xml<br/>
	 * image/gif<br/>
	 * image/pjpeg<br/>
	 * application/x-ms-xbap<br/>
	 * application/vnd.ms-excel<br/>
	 * application/vnd.ms-powerpoint<br/>
	 * application/msword<br/>
	 * 
	 * @param httpResponse
	 * @throws IOException
	 */
	protected void testContentTypeHeader(HttpServletResponse httpResponse) throws IOException {
		// 设置响应内容类型
		// httpResponse.setContentType("application/msword");// 设置相应内容为word文档
		httpResponse.setHeader("Content-Type", "image/jpeg");
		InputStream in = this.getServletConfig().getServletContext().getResourceAsStream("images/1.jpg");
		byte[] buffer = new byte[1024];
		int length = 0;
		OutputStream out = httpResponse.getOutputStream();
		while ((length = in.read(buffer)) != -1) {
			out.write(buffer, 0, length);
		}
	}

	/**
	 * Http响应头: Content-Encoding<br/>
	 * Http响应头: Content-Length<br/>
	 * 
	 * @param response
	 * @param httpResponse
	 * @throws IOException
	 */
	protected void testContentEncodingHeader(ServletResponse response, HttpServletResponse httpResponse)
			throws IOException {
		// 设置Content-Encoding响应头, 告诉浏览器数据压缩的格式
		String data = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
		System.out.println("原始数据大小为: " + data.getBytes().length);

		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		GZIPOutputStream zipOut = new GZIPOutputStream(byteOut);
		zipOut.write(data.getBytes());
		zipOut.close();

		// 得到压缩后的数据
		byte[] zipData = byteOut.toByteArray();
		httpResponse.setHeader("Content-Encoding", "gzip");
		httpResponse.setHeader("Content-Length", zipData.length + "");
		response.getOutputStream().write(zipData);
		System.out.println("压缩后数据大小为: " + zipData.length);
	}

	/**
	 * Http响应头: Refresh<br/>
	 * 
	 * @param httpResponse
	 */
	protected void testRefreshHeader(HttpServletResponse httpResponse) {
		// 浏览器每隔3秒刷新一次
		// httpResponse.setHeader("Refresh", "3");
		// 浏览器3秒后跳转到指定的URL
		httpResponse.setHeader("Refresh", "3, url='http://www.baidu.com'");
	}

	/**
	 * Http响应头: Content-Disposition<br/>
	 * 
	 * @param httpResponse
	 * @throws IOException
	 */
	protected void testContentDispositionHeader(HttpServletResponse httpResponse) throws IOException {
		httpResponse.setHeader("Content-Disposition", "attachment;fileName=response.jpg");
		InputStream in = this.getServletConfig().getServletContext().getResourceAsStream("images/1.jpg");
		byte[] buffer = new byte[1024];
		int length = 0;
		OutputStream out = httpResponse.getOutputStream();
		while ((length = in.read(buffer)) != -1) {
			out.write(buffer, 0, length);
		}
	}
}
